package com.xianzhiOA.service.impl;

import com.github.pagehelper.PageHelper;
import com.xianzhiOA.bean.XzAttendance;
import com.xianzhiOA.bean.XzAttendanceStateRecord;
import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.dao.XzAttendanceMapper;
import com.xianzhiOA.dao.XzAttendanceStateRecordMapper;
import com.xianzhiOA.dao.XzEmpMapper;
import com.xianzhiOA.service.XzAttendanceService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.*;


@Service("xzAttendanceService")
public class XzAttendanceServiceImpl implements XzAttendanceService {
    @Autowired
    private XzAttendanceMapper AttendanceMapper;
    @Autowired
    private XzAttendanceStateRecordMapper attendanceStateRecordMapper;


    @Override
    public int insert(XzAttendance xzAttendance) {

        Calendar date = Calendar.getInstance();
        date.setTime(xzAttendance.getDate());
        System.out.println("xzAttendance.getDate():"+xzAttendance.getDate());
        System.out.println("date:"+date);
        System.out.println("年: " + date.get(Calendar.YEAR));
        System.out.println("月: " + (date.get(Calendar.MONTH) + 1) + "");
        System.out.println("日: " + date.get(Calendar.DAY_OF_MONTH));
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
        System.out.println(dateFm.format(xzAttendance.getDate()));

        Date startWork=new Date();
        startWork=mystartWorkTime(xzAttendance.getDate());  //规定上班时间9点
        Date endWork=new Date();
        endWork=myendWorkTime(xzAttendance.getDate());  //规定下班时间5:30点

        //startWork.setTime("9:00");
        XzAttendanceStateRecord attendanceStateRecord =new XzAttendanceStateRecord();
        String AttendanceState=null;

        if(xzAttendance.getState() ==0){  //该天该人没有出勤，（休息或旷工）

            String aa=dateFm.format(xzAttendance.getDate());
            System.out.println("星期："+aa);


            if(dateFm.format(xzAttendance.getDate()).equals("星期六")  || dateFm.format(xzAttendance.getDate()).equals("星期日")){
                AttendanceState="1";    //休息

            }else {
                AttendanceState="2";  //旷工
            }
         }else {
               if(xzAttendance.getFirstTime() != null && xzAttendance.getLastTime() ==null){
                   //如果实际上班时间大于9:00则是迟到
                       if(xzAttendance.getFirstTime().getTime() >startWork.getTime()){
                           AttendanceState="4";
                       }else {
                           AttendanceState="3";        //没有迟到，但只打一次卡的情况是早退
                       }
               }
               else if (xzAttendance.getFirstTime() != null && xzAttendance.getLastTime() !=null){
                   if(xzAttendance.getFirstTime().getTime() >startWork.getTime()){     //如果实际上班时间大于9:00则是迟到
                       AttendanceState="4";
                   }else if (xzAttendance.getLastTime().getTime() < endWork.getTime()){     //实际下班打卡时间小于5：30，则是早退
                       AttendanceState="3";
                   }else {
                       AttendanceState="5";   //正常上班
                   }
               }

         }

         Map<String,Object> map=new HashMap<>();
        map.put("empNo",xzAttendance.getEmpNo());
        map.put("years",date.get(Calendar.YEAR));
        map.put("months",String.valueOf(date.get(Calendar.MONTH) + 1));
        map.put("days",date.get(Calendar.DAY_OF_MONTH));
        map.put("state",AttendanceState);

        if(attendanceStateRecordMapper.countByAttendance(map)>0){
           int a=  attendanceStateRecordMapper.updateAttendanceState(map);
        }else {
            int b = attendanceStateRecordMapper.insertAttendanceStateRecord(map);
        }

        return  AttendanceMapper.insert(xzAttendance);

    }

    //拼接上班时间
    public static Date mystartWorkTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 9);
        c.set(Calendar.MINUTE, 00);
        c.set(Calendar.SECOND, 00);
        c.set(Calendar.MILLISECOND, 000);
        return c.getTime();
    }

    //拼接下班时间
    public static Date myendWorkTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 5);
        c.set(Calendar.MINUTE, 30);
        c.set(Calendar.SECOND, 00);
        c.set(Calendar.MILLISECOND, 000);
        return c.getTime();
    }




    //
//    @Override
//    public int deleteByPrimaryKey(Long attendanceId) {
//        return AttendanceMapper.deleteByPrimaryKey(attendanceId);
//    }
//
//    @Override
//    public int phoneyDeleteByPrimaryKey(XzAttendance xzAttendance) {
//        return AttendanceMapper.phoneyDeleteByPrimaryKey(xzAttendance);
//    }
//
//    @Override
//    public XzAttendance findById(Long attendanceId) {
//        return  AttendanceMapper.selectByPrimaryKey(attendanceId);
//    }
//
//    @Override
//    public int updateByPrimaryKey(XzAttendance xzAttendance) {
//        return  AttendanceMapper.updateByPrimaryKey(xzAttendance);
//    }
//
    @Override
    public PageBean<XzAttendance> selAll(XzAttendance xzAttendance, Date beginDate, Date endDate, int page, int rows) {

        PageHelper.startPage(page,rows);
        Map<String,Object> map=MakeQ(xzAttendance);
        if(beginDate != null) map.put("beginDate",beginDate);
        if(endDate != null) map.put("endDate",endDate);

        List<XzAttendance> xzAttendanceList=new ArrayList<XzAttendance>();
        xzAttendanceList= AttendanceMapper.selAll(map);

        PageBean<XzAttendance> pb=new PageBean<>(xzAttendanceList);
        return  pb;
    }

    private  Map<String,Object> MakeQ(XzAttendance xzAttendance){
        Map<String,Object> hashMap=new HashMap<>();
        hashMap.put("attendanceId",xzAttendance.getAttendanceId());
        hashMap.put("empNo",xzAttendance.getEmpNo());
        hashMap.put("empName",xzAttendance.getXzEmps().getEmpName());
       hashMap.put("empAccount",xzAttendance.getXzEmps().getEmpAccount());
        hashMap.put("firstTime",xzAttendance.getFirstTime());
        hashMap.put("lastTime",xzAttendance.getLastTime());
        return  hashMap;
    }

    //根据选定的开始时间和结束时间和用户编号进行查询(用于判定需添加还是修改)
    @Override
    public XzAttendance selectByDayAndEmpNo(Date beginDate, Date endDate, int empNo) {
        System.out.println("进入service中,beginDate:"+beginDate+",endDate:"+endDate+",empNo:"+empNo);

        Map<String,Object> hashMap=new HashMap<>();
        if(beginDate != null) hashMap.put("beginDate",beginDate);
        if(endDate != null) hashMap.put("endDate",endDate);
        if(empNo != 0) hashMap.put("empNo",empNo);

        XzAttendance xzAttendance= null;
        try {
            System.out.println("hashMap:"+hashMap);
            xzAttendance = AttendanceMapper.selectByDayAndEmpNo(hashMap);
            System.out.println("未走catch");
        } catch (Exception e) {
            System.out.println("走了catch");
            e.printStackTrace();
            System.out.println("e："+e);
        }
        System.out.println("service中xzAttendance："+xzAttendance);
        return  xzAttendance;
    }





    // 获得当天0点时间
    public  Date getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获得当天24点时间
    public  Date getTimesnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return  cal.getTime();
    }
    //获取本周开始时间
    public  Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        return cal.getTime();
    }
    //获取本周末结束时间
    public   Date getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getTimesWeekmorning());
        cal.add(Calendar.DAY_OF_WEEK, 7);
        return cal.getTime();
    }
    //获取本月开始时间
    public  Date getTimesMonthmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return  cal.getTime();
    }
    //获取本月结束时间
    public  Date getTimesMonthnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTime();
    }




}
